from util.pic_video import *
from util.LabelPoints2ImagebyMayaParam import *
from util.visualize_utils import *
import os
import time
import pandas as pd
import json
import shutil

type_table = {
    '轿车': 'car',
    '巴士': 'bus',
    '货车': 'truck',
    '行人':'pedestrians',
    '挂车':'trailer',
    '其他':'other',
    '工程车辆':'Engineering_vehicles',
    '交通锥':'Traffic_cone',
    '两轮车':'motorcycle'
}

file='/media/king/MyPassport0/livox_truck_dataset_v2.0/data/'
# id=['11','12','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28']
# id=['11','12','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28']
# alone=[]
# for i in range(30):
#     alone.append('data'+str(i+1)+'_2020_12_24')

alone=os.listdir(file)
alone=sorted(alone)
for alone_ in alone:
    if '.' in alone_:
        continue
    data_file = file + alone_
    print(data_file)
    # 解析pkl文件成json写入label
    label_path=data_file + '/pkl_label_json'
    if not os.path.exists(label_path):
        os.makedirs(label_path)
    data_temp = pd.read_pickle(data_file + '/' + 'livox_truck_data.pkl')
    for file_id in range(data_temp.__len__()):
        datas_temp = []
        file_name = data_temp[file_id]['point_cloud']['lidar_path'].split('/')[-1][0:-4] + '.json'
        for obj_id in range(data_temp[file_id]['annos']['name'].__len__()):
            data1 = {'psr':
                         {'position':
                            {'x': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][0],
                            'y': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][1],
                            'z': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][2]},
                        'scale':
                            {'x': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][3],
                            'y': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][4],
                            'z': data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][5]},
                        'rotation':
                            {'x': 0,
                            'y': 0,
                            'z':data_temp[file_id]['annos']['gt_boxes_lidar'][obj_id][6]}},
                     'obj_type': data_temp[file_id]['annos']['name'][obj_id],
                     'obj_id': obj_id,
                     }
            datas_temp.append(data1)
        with open(label_path+ '/' + file_name, 'w') as f:
            json.dump(datas_temp, f)

    # # 解析整体gt文件成json写入gtlabel
    # gtlabel_path=data_file + '/gt_label_json'
    # if not os.path.exists(gtlabel_path):
    #     os.makedirs(gtlabel_path)
    # ground_truth = data_file + '/ground_truth/'
    # gt_files=os.listdir(ground_truth)
    # with open(ground_truth+gt_files[0], 'r') as f:
    #     data_temp = json.load(f)
    # for file_id in range(data_temp.__len__()):
    #     datas_temp = []
    #     file_name = data_temp[file_id]['3D_source'].split('/')[-1][0:-4] + '.json'
    #     for obj_id in range(data_temp[file_id]['框'].__len__()):
    #         data1 = {'psr':
    #                      {'position':
    #                           data_temp[file_id]['框'][obj_id]['center'],
    #                       'scale': {'x': data_temp[file_id]['框'][obj_id]['width'],
    #                                 'y': data_temp[file_id]['框'][obj_id]['height'],
    #                                 'z': data_temp[file_id]['框'][obj_id]['depth']},
    #                       'rotation': data_temp[file_id]['框'][obj_id]['rotation']},
    #                  'obj_type': type_table[data_temp[file_id]['框'][obj_id]['attributes']['类型'][0]],
    #                  'obj_id': data_temp[file_id]['框'][obj_id]['id']
    #                  }
    #         datas_temp.append(data1)
    #     with open(gtlabel_path+ '/' + file_name, 'w') as f:
    #         json.dump(datas_temp, f)

    # 复制data内.bin文件到lidar
    lidar_path=data_file+'/lidar/'
    if not os.path.exists(lidar_path):
        shutil.copytree(data_file+'/data_bin/',lidar_path)

    # # 复制pic_040302_release/p0文件到image
    # image_path=data_file+'/image/'
    # if not os.path.exists(image_path):
    #     shutil.copytree(data_file+'/pic_040302_release/p0/',image_path)